/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "窗口.h"

#include "intern/Window.h"
#include "系统/系统平台.h"

using namespace std;


static S_Window* g_当前激活窗口 = nullptr;


S_Window* f_window_创建(std::string 标题, S_设备环境& Ctx, bool 显示, ivec4 rect) {
	S_Window* 窗口 = new S_Window(Ctx);

	窗口->f_创建窗口(标题, rect.x, rect.y, rect.z, rect.w, 显示);
	窗口->f_创建绘制面(Ctx, { rect.z, rect.w });
	//g_当前激活窗口 = 窗口;
	return 窗口;
}



S_Window* f_window_创建(S_设备环境& Ctx, std::string 标题, ivec4 rect) {
	S_Window* 窗口 = new S_Window(Ctx);
	f_window_创建SDL窗口(窗口, "MBT SDL窗口", _Vec2(rect.z, rect.w));

	//窗口->f_创建绘制面(Ctx);

	return 窗口;
}

ivec2 f_window_get焦点窗口大小() {
	if (g_当前激活窗口) {
		return g_当前激活窗口->f_getSize();
	}
	return {};
}

S_Window* f_创建VK窗口(string 标题, S_设备环境& Ctx, int32 x, int32 y, int32 w, int32 h, string dllPath) {
	S_Window* 窗口 = new S_Window(Ctx);

	窗口->f_创建窗口(标题, x, y, w, h, 0, dllPath);
	return 窗口;
}

S_Window* f_创建DX窗口(string 标题, S_设备环境& Ctx, int32 x, int32 y, int32 w, int32 h, string dllPath) {
	S_Window* 窗口 = new S_Window(Ctx);

	窗口->f_创建窗口(标题, x, y, 窗口->m_Size.x, 窗口->m_Size.y, 0, dllPath);
	return 窗口;
}

void f_window_销毁(S_Window* 窗口) {
	
	delete 窗口;
}

void f_window_set输入法窗口位置(const ivec2& pos) {

}

void f_window_set窗口标题(S_Window* window, std::wstring 标题) {
	window->f_set标题(标题);
}

vec2 f_window_get当前窗口大小() {
	return { float32(g_当前激活窗口->m_Size.x), float32(g_当前激活窗口->m_Size.y) };
	return vec2();
}

void f_window_清除UI(S_Window* window) {
	window->m_窗口GM->m_根部件切换组.clear();
	window->m_窗口GM->m_Root = nullptr;
}

void f_window_安装UI(S_Window* window, C_Widget* ui) {
	window->f_安装UI(ui);
	//window->m_窗口GM->m_Window = window;
}

S_UI渲染环境* f_window_getUI环境(S_Window* window) {
	return window->m_UIctx;
}

C_Widget管理* f_window_getUI管理(S_Window* window) {
	return window->m_窗口GM;
}

void f_window_set显示(S_Window* window, bool 显示) {
	window->f_显示(显示);
}

void f_window_加载图标(S_Window* window, std::string 组名称, const std::filesystem::path& path) {
	window->m_窗口GM->f_加载图标(组名称, path.generic_string());
}

void f_window_加载字体(S_Window* window, std::string 字体, uint16 字号) {
	auto ctx = f_window_getUI环境(window);

	ctx->m_文字字号 = 字号;
	std::string 字体文件 = (f_sys_get字体文件目录() / 字体).string();

	f_ui加载字体(ctx,
		{
			{
				{
					//{0x0000, 0x007F}, {0x4E00, 0x9FA5},
					{0x0000, 0xFFFF},
					//0xFFFF,
				},

				字体文件,
				ctx->m_文字字号,
				字体
			}
		}
	);
}




void f_window_轮询(S_Window** window, uint32 num, bool& 退出, bool& 暂停) {
	//bool 累计渲染 = false;
	MSG msg = { 0 };
	
	//if (PeekMessage(&msg, GetConsoleWindow(), 0, 0, PM_REMOVE)) {
	//	if (msg.message == WM_DESTROY || msg.message == WM_QUIT || msg.message == WM_CLOSE) 
	//		return ;
	//
	//	//if (msg.hwnd == GetForegroundWindow()) {
	//	//	
	//	//}
	//	if (msg.message != WM_SYSKEYUP) {
	//		TranslateMessage(&msg);	
	//		DispatchMessage(&msg);	
	//	}
	//}


	//for (uint32 i = 0; i < num; ++i) {
	//	//auto info = GetMessage(&msg, (HWND)window[i]->m_窗口钩柄, 0, 0);
	//
	//	if (PeekMessage(&msg, (HWND)window[i]->m_窗口钩柄, 0, 0, PM_REMOVE)) {
	//		if (msg.message == WM_DESTROY || msg.message == WM_QUIT || msg.message == WM_CLOSE) 
	//			return ;
	//		if (msg.message != WM_SYSKEYUP) {
	//			TranslateMessage(&msg);	
	//			DispatchMessage(&msg);	
	//		}
	//	}
	//
	//	//窗口有消息 |= info;
	//}
	//if (!窗口有消息) {
	//	
	//}


	//if (GetMessage(&msg, 0, 0, 0)) {
	if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
		if (msg.message == WM_DESTROY || msg.message == WM_QUIT || msg.message == WM_CLOSE) 
			return ;
	
		//if (msg.hwnd == GetForegroundWindow()) {
		//	
		//}
		if (msg.message != WM_SYSKEYUP) {
			TranslateMessage(&msg);	
			DispatchMessage(&msg);	
		}
	}
	

	for (uint32 i = 0; i < num; ++i) {
		window[i]->m_窗口GM->有输入事件 = false;
		window[i]->m_窗口GM->键盘按键向下传递 = E_事件是否传递::e_事件传递_继续;

		退出 |= window[i]->f_轮询窗口事件(暂停);

		暂停 |= window[i]->m_窗口GM->有输入事件;
	}


	//std::cout<< 暂停 << "A\n";
	//To_跳过系统按键:

	if (暂停 == false) {
		WaitMessage();
	}

	//std::cout<<"B\n";
}

void f_window_等待() {
	WaitMessage();
}

void f_window_触发消息(S_Window** window, uint32 num) {
	for (uint32 i = 0; i < num; ++i) {
		PostMessage((HWND)window[i]->m_窗口钩柄, 0, 0, 0);
	}
}